<script setup>
import { ref } from "vue";
import { User, Lock } from "@element-plus/icons-vue";
import { regUserApi } from "@/api/user";
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";
const router = useRouter();

const regForm = ref({
  username: "",
  password: "",
  repassword: "",
});
const rules = {
  username: [
    // 用户名是1~10位英文数字组成

    { required: true, message: "用户名称不能为空", trigger: "blur" },
    {
      pattern: /^[a-zA-Z0-9]{1,10}$/,
      message: "用户名称是1~10位英文数字组成",
      trigger: "blur",
    },
  ],
  password: [
    { required: true, message: "用户密码不能为空", trigger: "blur" },
    // 密码6~10位的非空字符
    {
      pattern: /^\S{6,10}$/,
      message: "密码应是6~10位的非空字符",
      trigger: "blur",
    },
  ],
  repassword: [
    { required: true, message: "确认密码不能为空", trigger: "blur" },
    // 确认密码与密码是一致
    // rule没有什么用
    // value是当前表单项的数据
    // callback不管成功或失败都要调用
    {
      validator(rule, value, callback) {
        if (value === regForm.value.password) {
          //相同 放行
          callback();
        } else {
          callback(new Error("确认密码与密码不一致"));
        }
      },
      trigger: "blur",
    },
  ],
};
const regFormRef = ref(null);

const register = () => {
  // 兜底校验
  regFormRef.value.validate(async (valid) => {
    if (!valid) return;

    const { data: res } = await regUserApi(regForm.value);
    if (res.code !== 0) return ElMessage.error(res.message);
    ElMessage.success(res.message);

    //跳转到登录页面 稍等
  });
};
</script>

<template>
  <div class="reg-container">
    <div class="reg-box">
      <div class="title-box"></div>
      <!-- 注册表单 element-puls中的组件el-form 
        三层的结构：el-form  el-form-item  el-input
        四个属性： model  rules   prop   v-model
      -->
      <el-form :model="regForm" :rules="rules" ref="regFormRef">
        <el-form-item prop="username">
          <el-input
            v-model="regForm.username"
            placeholder="请输入用户名称"
            :prefix-icon="User"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            v-model="regForm.password"
            placeholder="请输入用户密码"
            :prefix-icon="Lock"
          ></el-input>
        </el-form-item>
        <el-form-item prop="repassword">
          <el-input
            type="password"
            v-model="regForm.repassword"
            placeholder="请输入确认密码"
            :prefix-icon="Lock"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="regBtn" @click="register"
            >注册</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-link :underline="false" @click="router.push('/login')"
            >去登录</el-link
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<style scoped lang="less">
.reg-container {
  height: 100%;
  background: url("@/assets/images/login_bg.jpg") center;
  background-size: cover;

  .reg-box {
    width: 400px;
    height: 335px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    padding: 0 20px;
    box-sizing: border-box;

    .title-box {
      height: 60px;
      background: url("../../assets/images/login_title.png") center no-repeat;
    }

    .el-input {
      height: 40px;
    }

    .regBtn {
      width: 100%;
      height: 40px;
    }
  }
}
</style>
